{extend name="lib/common_list" /}
{block name="title"}管理员管理{/block}
{block name="css"}{/block}
{block name="javascript"}
    <script>
        function initTable() {
            //先销毁表格
            $('#tb').bootstrapTable('destroy');
            //初始化表格,动态从服务器加载数据
            $("#tb").bootstrapTable({
                method: "Post",  //使用Post请求到服务器获取数据
                url: "{:url('manager/getList')}", //获取数据的Servlet地址
                striped: true,  //表格显示条纹
                pagination: true, //启动分页
                pageSize: 20,  //每页显示的记录数
                pageNumber: 1, //当前第几页
                pageList: [20, 50],  //记录数可选列表
                search: false,  //是否启用查询
                sortable: true, //是否启用排序
                sortOrder: "asc", //排序方式
                //showColumns: true,  //显示下拉框勾选要显示的列
                //showRefresh: true,  //显示刷新按钮
                //sortName: "PullTime",
                sidePagination: "server", //表示服务端请求
                //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                //设置为limit可以获取limit, offset, search, sort, order
                queryParamsType: "limit",
                contentType: "application/x-www-form-urlencoded",
                queryParams: function queryParams(params) {   //设置查询参数
                    var param = {
                        limit: params.limit,
                        offset: params.offset,
                        sortName: params.sort,
                        sortOrder: params.order,
                        keyword: $("input[name=keyword]").val()
                    };
                    return param;
                },
                onLoadSuccess: function () {  //加载成功时执行

                },
                onLoadError: function () {  //加载失败时执行
                    layer.msg("加载数据失败", { time: 1500, icon: 2 });
                },
                rowStyle: function (row, index) {
                    var strclass = "";
                    if (row.status==1) {
                        strclass = 'danger';
                    }                
                    else {
                        return {};
                    }
                    return { classes: strclass }
                }
            });
        }
        
        $(document).ready(function () {
            //调用函数，初始化表格
            initTable();
            //点击查询按钮
            $("#search").bind("click", initTable);
            {if check_permissioncode("1001001001")}
                //点击新增按钮
                $("#btnAdd").bind("click", add);
            {/if}
        });

        function initAcTable() {
            initTable();
        }

        {if check_permissioncode("1001001001")}
            var layer_add_index;
            function add() {
                layer_add_index = layer.open({
                    type: 2,
                    title: "新增用户",
                    area:['70%', '80%'],
                    content:"{:url('manager/add')}"
                })
            }
        {/if}

        {if check_permissioncode("1001001002")}
            var layer_edit_index;
            function edit(id) {
                layer_edit_index = layer.open({
                    type: 2,
                    title: "修改用户",
                    area:['70%', '80%'],
                    content:"{:url('manager/edit')}?id="+id
                })
            }
        {/if}

        {if check_permissioncode("1001001003")}
            function Enable(id,status) {
                layer.confirm('你确定要'+(status==0?"【启用】":"【禁用】")+'该帐号吗？', {
                    btn: ['是', '否'] //按钮
                }, function () {
                    var load_index = layer.load(0, { shade: false });
                    $.ajax({
                        type: "post",
                        url: "{:url('manager/enable')}",
                        data: { id: id, status: status },
                        dataType: "json",
                        success: function (result) {
                            layer.close(load_index);
                            if (result.code == 0) {
                                var ly = layer.alert((status==0?"启用":"禁用")+'成功！', function () {
                                    initTable();
                                    layer.close(ly);
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        },
                        error:function(xhr,textStatus){
                            layer.close(load_index);
                            layer.alert('服务器繁忙！');
                        }
                    });
                });
            }
        {/if}

        {if check_permissioncode("1001001004")}
            var layer_userroles_index;
            function UserRoles(id) {
                layer_userroles_index = layer.open({
                    type: 2,
                    title: "分配角色",
                    shadeClose: false,
                    shade: 0.6,
                    skin: 'layui-layer-rim',
                    area: ["650px", '300px'],
                    //scrollbar: false,
                    content: ["{:url('role/assignroles')}?id=" + id, "no"]
                })
            }
        {/if}

        function statusFormat(index, row) {            
            return row.status==1 ? "禁用" : "启用";
        }

        function dateFormat(index, row) {
            if (index == null || index == 0) return;
            var date = new Date(index * 1000);  
            var y = date.getFullYear();    
            var m = date.getMonth() + 1;    
            m = m < 10 ? ('0' + m) : m;    
            var d = date.getDate();    
            d = d < 10 ? ('0' + d) : d;    
            var h = date.getHours();  
            h = h < 10 ? ('0' + h) : h;  
            var minute = date.getMinutes();  
            var second = date.getSeconds();  
            minute = minute < 10 ? ('0' + minute) : minute;    
            second = second < 10 ? ('0' + second) : second;   
            return y + '-' + m + '-' + d+' '+h+':'+minute; 
        }

        function operateFormatter(index, row) {
            var html = "";
            if ({$uid} == 1) {
                html += "<a href=\"javascript:edit(" + row.id + ");\"><i class=\"fa fa-edit text-navy\"></i>修改</a>";
                if (row.id != {$uid}) {
                    if (row.status==1) {
                        html += "&nbsp;&nbsp;<a href=\"javascript:Enable(" + row.id + ",0);\"><i class=\"fa fa-check-circle-o text-green\"></i>启用</a>";
                    }else {
                        html += "&nbsp;&nbsp;<a href=\"javascript:Enable(" + row.id + ",1);\"><i class=\"fa fa-ban text-danger\"></i>禁用</a>";
                    }
                    html += "&nbsp;&nbsp;<a href=\"javascript:UserRoles(" + row.id + ");\"><i class=\"fa fa-sitemap text-danger\"></i>分配角色</a>";
                }
            }else {
                if (row.id != {$uid} && row.roleid != 1) {
                    {if check_permissioncode("1001001002")}
                        html += "<a href=\"javascript:edit(" + row.id + ");\"><i class=\"fa fa-edit text-navy\"></i>修改</a>";
                    {/if}
                    {if check_permissioncode("1001001003")}
                        if (row.status==1) {
                            html += "&nbsp;&nbsp;<a href=\"javascript:Enable(" + row.id + ",0);\"><i class=\"fa fa-check-circle-o text-green\"></i>启用</a>";
                        }else {
                            html += "&nbsp;&nbsp;<a href=\"javascript:Enable(" + row.id + ",1);\"><i class=\"fa fa-ban text-danger\"></i>禁用</a>";
                        }
                    {/if}
                    {if check_permissioncode("1001001004")}
                        html += "&nbsp;&nbsp;<a href=\"javascript:UserRoles(" + row.id + ");\"><i class=\"fa fa-sitemap text-danger\"></i>分配角色</a>";
                    {/if}
                }
            }
            return html;
        }
    </script>
{/block}
{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>管理员列表</h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-2 m-b-xs">
                            {if check_permissioncode("1001001001")}
                            <span>
                                <button type="button" class="btn btn-primary btn-sm" id="btnAdd"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                            </span>
                            {/if}
                        </div>
                        <div class="col-sm-2 col-sm-offset-2">
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <input type="text" placeholder="请输入登录名/姓名" class="input-sm form-control" id="keyword" name="keyword">
                                    <button type="button" class="btn btn-sm btn-primary" id="search">搜索</button>
                                </span>
                            </div>
                        </div>
                        <span id="refresh" class="fa fa-refresh fa-spin" title="刷新" onclick="javascript:$('#tb').bootstrapTable('refresh')"></span>
                    </div>
                    <table data-toggle="table" id="tb" data-pagination="true">
                        <thead>
                            <tr>
                                <th data-align="center" data-field="user_name" data-sortable="true">登录名</th>
                                <th data-align="center" data-field="real_name" data-sortable="true">姓名</th>
                                <th data-align="center" data-field="role_name" data-sortable="true">角色</th>
                                <th data-align="center" data-field="status" data-formatter="statusFormat" data-sortable="true">状态</th>
                                <th data-align="center" data-field="last_login_ip" data-sortable="true">最后登录IP</th>
                                <th data-align="center" data-field="last_login_time" data-formatter="dateFormat" data-sortable="true">最后登录时间</th>
                                <th data-align="center" data-formatter="operateFormatter">操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}